package graphic;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import mouseEvents.ImageC;
import Graph.Graph;
import Graph.optimalWay;

public class MyPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Graph City = new Graph();
	public int[][] graph;
	int intensity;
	int green = 0;
	int blue = 0;
	int[][] CarsOnRoad;
	boolean fl = true;
	public int number;
	public int way [] = new int [100];
	public int length;
	public Point point [];
	public int maxX = 0;
	public int maxY = 0;
	public double kx = 0;
	public double ky = 0;
	public AllCars Cars;
	private int j = 0;
	private ImageC img;
	optimalWay Map = new optimalWay (City);
	public MyPanel(Graph City){
		this.City = City;
		CarsOnRoad = new int[City.getNumber()][City.getNumber()];
		CarsOnRoad = City.getCarsOnRoads();
		point = City.getPoint();
		graph = City.getGraph();
		number = City.getNumber();
		Cars = new AllCars(City);
		img = new ImageC();
	}
	
	
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		img.paintComponent(g);
		
		g.setColor(Color.black);
		if(fl){
		for(int i = 0; i < number; i++){
			if(point[i].x > maxX){
				maxX = point[i].x;
			}
			if(point[i].y > maxY ){
				maxY = point[i].y;
			}
		}
		kx = ((double) 800) / ((double)maxX);
		ky = ((double) 600) / ((double)maxY);
		for(int i = 0; i < number; i++){
			point[i].x = (int) ((double) point[i].x * kx); 
			point[i].y = (int) ((double) point[i].y * ky);
		}
		fl = false;
		}
		Graphics2D g1 = (Graphics2D) g;
		g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g1.setColor(Color.black);
		BasicStroke pen1 = new BasicStroke(10);
		g1.setStroke(pen1);
		BasicStroke pen2 = new BasicStroke(1.4f, 0, 0, 1f, new float[] {4f}, 0f);
		
		for(int i = 0; i < number; i++){
			for(int j = 0; j < number; j++){
				if(graph[i][j] < City.getNoRoad()){
					intensity = CarsOnRoad[i][j] + CarsOnRoad[j][i];
					
					intensity = (int)((double) 255 / 12 * (double) intensity);
					if(intensity > 220){
						green = intensity - 222;
						intensity = 222;
						if(green > 220){
							blue = green -222;
							green = 220;
							if(blue > 100){blue = 100;}
						}
					}
					Color t = new Color(intensity,green,blue);
					g1.setColor(t);
					
					g1.drawLine(point[i].x,  point[i].y, point[j].x,  point[j].y);
					
					green = 0;
					blue = 0;
				}
			}
		}
		g1.setColor(Color.WHITE);
		g1.setStroke (pen2);
		for(int i = 0; i < number; i++){
			for(int j = 0; j < number; j++){
				if(graph[i][j] < City.getNoRoad()){
					g1.drawLine(point[i].x,  point[i].y, point[j].x,  point[j].y);
				}
			}
		}
		
		g1.setColor(Color.BLACK);
		for(int i = 0; i < number; i++){
			g1.fillOval(point[i].x - 10, point[i].y - 10, 20, 20);
			g1.setColor(Color.red);
			g1.drawString("" + i + "", point[i].x + 10, point[i].y + 10);
			g1.setColor(Color.BLACK);
		}
		
		Cars.paintComponent(g);
		++j;
		AllCars.addRandomCar();
		if(j % 100 == 0){
			Map.refresh(City);
			j = 0;
		}
	}
	
	public void setmaxCars(int maxCars){
		Cars.setmaxCars(maxCars);
		
	}
	
}
